﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Mirror;

public class LoginPanel :UIBase
{
    private InputField iptAccount;
    private InputField iptPassword;
    private Button btnLogin;
    private Button btnRegister;
    private Button btnClose;
  
    void Start()
    {
        uiType = UIType.LoginPanel;
        UIManager.instance.Add(this);
        iptAccount = transform.Find("txtName/iptAccount").GetComponent<InputField>();
        iptPassword=transform.Find("txtPassword/iptpassword").GetComponent<InputField>();
        btnLogin = transform.Find("btnLogin").GetComponent<Button>();
        btnRegister = transform.Find("btnRegister").GetComponent<Button>();
        btnClose= transform.Find("btnClose").GetComponent<Button>();

     
        btnLogin.onClick.AddListener(OnBtnLoginClick);
        btnRegister.onClick.AddListener(OnBtnRegister);
        btnClose.onClick.AddListener(OnBtnClose);
        this.gameObject.SetActive(true);
        ShowUI();
    }

    private void OnBtnClose()
    {
        Application.Quit();
    }

    private void OnBtnRegister()
    {
        HideUI();
       UIManager.instance.ShowPanel(UIType.RegisterPanel);
    }

    private void OnBtnLoginClick()
    {
        //本地化校验
        if (!LocalVerification()) return;

        //给服务器发送注册消息
        NetMessage msg = new NetMessage() 
        { 
            AreaCode = AreaCode.Account,
            //OperationCode=OperationCode.Login,
            //ReturnCode =100,
            //SubCode=101,
            Value= "看看有没有你"

        };
        //string acctount = iptAccount.text;
        //string pwd = iptPassword.text;
        //msg.AreaCode = AreaCode.Account;
        //msg.OperationCode = OperationCode.Login;
        //msg.ReturnCode = 100;
        //msg.SubCode = 100;
        //msg.Value = "看看有没有你";

        Debug.Log("打印构造的结构" + msg.Value);
       // Debug.Log("打印msg.ReturnCode" + msg.ReturnCode);
        NetworkClient.Send(msg);
       
    }

    /// <summary>
    /// 账号本地校验
    /// </summary>
    /// <returns></returns>
    bool LocalVerification()
    {
        if (string.IsNullOrEmpty(iptAccount.text) || string.IsNullOrEmpty(iptPassword.text))
        {
            ShowTip("账号或密码不能为空!");
            return false;
        }
        return true;
    }

}
